<template>
  <div class="app-container">
    <upload-excel :on-success="upload" />
  </div>
</template>

<script>
import { batchImport } from '@/api/employee'
export default {
  methods: {
    async   upload({ results, header }) {
      // console.log(results)
      if (this.$route.query.type === 'employee') {
        this.employeesImport(results, header)
      }
    },
    formate(days) {
      // 获取1990年时间 Excel是从1900年开始计算的
      const oldTime = new Date('1899-12-31 0:0:0')
      // 时间戳
      const date = new Date(oldTime.getTime() + days * 24 * 60 * 60 * 1000)
      // 时间戳转化成时间
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
    },
    async employeesImport(results, header) {
      // header:手机号 姓名 入职时间 转正日期 工号
      // results: 入职日期:43535 姓名:"高大山" 工号:20089 手机号:13041139879 转正日期:43719
      const enList = []
      const arr = ['mobile', 'username', 'timeOfEntry', 'correctionTime', 'workNumber']
      results.forEach(item => {
        const enObject = {}
        header.forEach((key, index) => {
          if (key.includes('日期')) {
            enObject[arr[index]] = this.formate(item[key])
          } else {
            enObject[arr[index]] = item[key]
          }
        })
        enList.push(enObject)
      })
      await batchImport(enList)
      this.$message.success('导出成功')
      this.$router.back()
    }
  }
}
</script>

<style lang="scss" scoped></style>
